/**
 * 
 */
package com.mvn.account.email;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 *
 * @author wanghy
 * @since 2014-2-12
 * 
 */
public class AccountEmailServiceImpl implements AccountEmailService {

	private JavaMailSender javaMailSender;
	
	private String systemEmail;
	
	/* (non-Javadoc)
	 * @see com.whily.mvn.account.account.email.AccountEmailService#sendMail(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void sendMail(String to, String subject, String htmlText)
			throws AccountEmailException {
		try {
			MimeMessage msg = javaMailSender.createMimeMessage();
			MimeMessageHelper msgHelper = new MimeMessageHelper(msg);
			msgHelper.setFrom(systemEmail);
			msgHelper.setTo(to);
			msgHelper.setSubject(subject);
			msgHelper.setText(htmlText, true);
			
			javaMailSender.send(msg);
		} catch (MessagingException me) {
			throw new AccountEmailException("Faild to send mail.", me);
		}
	}

	/**
	 * @return the javaMailSender
	 */
	public JavaMailSender getJavaMailSender() {
		return javaMailSender;
	}

	/**
	 * @param javaMailSender the javaMailSender to set
	 */
	public void setJavaMailSender(JavaMailSender javaMailSender) {
		this.javaMailSender = javaMailSender;
	}

	/**
	 * @return the systemEmail
	 */
	public String getSystemEmail() {
		return systemEmail;
	}

	/**
	 * @param systemEmail the systemEmail to set
	 */
	public void setSystemEmail(String systemEmail) {
		this.systemEmail = systemEmail;
	}

}
